package com.rigi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rigi.common.R;
import com.rigi.entity.Category;
import com.rigi.service.CategoryService;
import com.rigi.service.DishService;
import com.rigi.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;


    /**
     * 添加分类
     *
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        boolean save = categoryService.save(category);
        if (save) {
            return R.success("添加套餐成功");
        }
        return R.error("添加套餐失败");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> page(Integer page, Integer pageSize) {
        Page<Category> categoryPage = new Page<>(page, pageSize);
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("sort");
        Page<Category> page1 = categoryService.page(categoryPage, wrapper);
        return R.success(page1);

    }

    /**
     * 删除分类
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids) {
        categoryService.remove(ids);
        return R.success("删除成功");
    }

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        boolean update = categoryService.updateById(category);
        if (update) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    /**
     * 查询所有菜品分类
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(@RequestParam(required = false) String type) {
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        if (type!=null){
            wrapper.eq("type", type);
        }

        wrapper.orderByAsc("sort").orderByDesc("update_time");
        List<Category> list = categoryService.list(wrapper);
        return R.success(list);
    }
}
